package com.lzx.applib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.lzx.applib.R;
import com.lzx.applib.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lzx on 16/12/05 0005.
 */
public class TextTagView extends LinearLayout {

    public static final String TAG = "TextTagView";

    public static final int TAG_ALIGN_LEFT = 0;
    public static final int TAG_ALIGN_JUSTIFY = 1;

    private List<String> tags = new ArrayList<>();
    private OnClickListener clickListener;
    private boolean buildView = false;

    private int spaceHorizontal, spaceVertical;
    private int tagPadding, tagPaddingLeft, tagPaddingRight, tagPaddingTop, tagPaddingBottom;
    private int tagAlign;
    ShapeTextView demo;

    public TextTagView(Context context) {
        this(context, null);
    }

    public TextTagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        setDividerDrawable(getResources().getDrawable(R.drawable.backbtn_normal));
        demo = new ShapeTextView(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextTagView);
        spaceHorizontal = array.getDimensionPixelOffset(R.styleable.TextTagView_spaceHorizontal, 0);
        spaceVertical = array.getDimensionPixelOffset(R.styleable.TextTagView_spaceVertical, 0);

        tagPadding = array.getDimensionPixelOffset(R.styleable.TextTagView_tagPadding, 0);
        tagPaddingLeft = array.getDimensionPixelOffset(R.styleable.TextTagView_tagPaddingLeft, tagPadding);
        tagPaddingRight = array.getDimensionPixelOffset(R.styleable.TextTagView_tagPaddingRight, tagPadding);
        tagPaddingTop = array.getDimensionPixelOffset(R.styleable.TextTagView_tagPaddingTop, tagPadding);
        tagPaddingBottom = array.getDimensionPixelOffset(R.styleable.TextTagView_tagPaddingBottom, tagPadding);

        tagAlign = array.getInt(R.styleable.TextTagView_tagAlign, TAG_ALIGN_LEFT);
        array.recycle();

        setMinimumHeight(1);
    }

    public void setTags(List<String> tags) {
        if (tags == null || tags.isEmpty()) {
            setVisibility(View.GONE);
            return;
        }
        postInvalidate();
        LogUtil.d(TAG, "setTags");
        this.tags = tags;
        buildView = false;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        LogUtil.d(TAG, "onDraw");
        super.onDraw(canvas);
        if (!buildView && tags.size()>0) {
            buildView();
        }
    }

    private void buildView() {
        LogUtil.d(TAG, "buildView");
        removeAllViews();

        LinearLayout layout = null;
        int width = getWidth();
        int offset = 0;
        for(String tag : tags) {
            ShapeTextView shapeTextView = new ShapeTextView(getContext());
            shapeTextView.copy(demo);
            shapeTextView.setText(tag);
            shapeTextView.setPadding(tagPaddingLeft, tagPaddingTop, tagPaddingRight, tagPaddingBottom);
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            shapeTextView.setLayoutParams(lp);
            shapeTextView.setOnClickListener(clickListener);

            int measureWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
            int measureHeight = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
            shapeTextView.measure(measureWidth, measureHeight);
            int textWidth = shapeTextView.getMeasuredWidth();

            if (offset == 0 || offset+textWidth+spaceHorizontal > width) {
                if (layout != null) {
                    dealWithRemainWidth(width-offset, layout);
                }
                layout = getTagLayout();
                lp.setMargins(0, 0, 0, 0);
                layout.addView(shapeTextView, lp);
                offset = textWidth;
            } else {
                offset += textWidth + spaceHorizontal;
                lp.setMargins(spaceHorizontal, 0, 0, 0);
                layout.addView(shapeTextView, lp);
            }
        }
        buildView = true;
    }

    private LinearLayout getTagLayout() {
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 0, spaceVertical);
        addView(layout, lp);
        return layout;
    }

    private void dealWithRemainWidth(int remainWidth, LinearLayout layout) {
        if (tagAlign == TAG_ALIGN_LEFT) return;

        if (tagAlign == TAG_ALIGN_JUSTIFY) {
            int count = layout.getChildCount();
            if (count == 1) return;

            int remain = remainWidth/(count-1);
            for (int i = 1; i < layout.getChildCount(); i++) {
                View view = layout.getChildAt(i);
                LayoutParams lp = (LayoutParams) view.getLayoutParams();
                lp.leftMargin += remain;
                view.setLayoutParams(lp);
            }
        }
    }

    @Override
    public void setOnClickListener(OnClickListener listener) {
        this.clickListener = listener;
    }
}
